/**   
 * @Title: ContractSignatory.java 
 * @Package com.betterjr.modules.contract.service 
 * @Description: 签署信息服务
 * @author xuzx
 * @date 2017年10月27日 下午7:12:26  
 */
package com.betterjr.modules.contract.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.account.entity.CustOperatorInfo;
import com.betterjr.modules.account.service.CustOperatorService;
import com.betterjr.modules.contract.dao.ContractSignatoryMapper;
import com.betterjr.modules.contract.entity.ContractSignatory;
import com.betterjr.modules.customer.ICustMechBaseService;

/** 
 * @ClassName: ContractSignatory 
 * @Description: 签署信息服务
 * @author xuzx
 * @date 2017年10月27日 下午7:12:26 
 *  
 */
@Service
public class ContractSignatoryService extends BaseService<ContractSignatoryMapper, ContractSignatory> {
    public static String[] signatorys = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };

    @Resource
    private CustOperatorService custOperatorService;

    @Reference(interfaceClass = ICustMechBaseService.class)
    private ICustMechBaseService custMechBaseService;

    /**
     * 
     * @Title: addContractSignatory 
     * @Description: 插入协议清单的相关签署方信息
     * @param anCommonNo 签署机构编号 
     * @param anRequestNo 业务编号
     * @param protocolListId 协议清档编号
     * @return 插入信息
     * @throws 
     * @author xuzx
     * @date 2017年10月31日 下午1:35:53
     */

    public void addContractSignatory(final String anCommonNo, final String anRequestNo, final Long protocolListId) {
        CustOperatorInfo custOperatorInfo = new CustOperatorInfo();
        String[] sourceStrArray = anCommonNo.split(",");
        if (StringUtils.isNoneBlank(anCommonNo)) {
            for (int i = 0; i < sourceStrArray.length; i++) {
                final ContractSignatory contractSignatory = new ContractSignatory();
                contractSignatory.initValue();
                contractSignatory.setRequestno(anRequestNo);
                contractSignatory.setProtocolListId(protocolListId);
                custOperatorInfo = custOperatorService.findDefaultOperator(
                        custMechBaseService.findBaseInfo(Long.valueOf(sourceStrArray[i])).getOperOrg());
                contractSignatory.setSignatoryOrganizationId(sourceStrArray[i]);
                contractSignatory.setName(custOperatorInfo.getName());
                contractSignatory.setEmail(custOperatorInfo.getEmail());
                contractSignatory.setMobileNo(custOperatorInfo.getMobileNo());
                contractSignatory.setSignatoryOrganization(
                        custMechBaseService.findBaseInfo(Long.valueOf(sourceStrArray[i])).getCustName());
                contractSignatory.setSignatory(signatorys[i]);
                this.insert(contractSignatory);

            }
        }

    }

    /**
     * 
     * @Title: delContractSignatory 
     * @Description: 根据编号删除签署信息 
     * @param anId
     * @return 根据编号删除签署信息 
     * @throws 
     * @author xuzx
     * @date 2017年10月31日 下午1:35:14
     */
    public int delContractSignatory(final Long anId) {
        return this.deleteByPrimaryKey(anId);
    }

    /**
     * 
     * @Title: delAssociatedSignature 
     * @Description: 删除协议清单关联签署方式
     * @param anContractProtocoListId
     * @return 删除协议清单关联签署方式
     * @throws 
     * @author xuzx
     * @date 2017年10月31日 下午1:34:38
     */
    public int delAssociatedSignature(final Long anContractProtocoListId) {
        Map<String, Object> anMap = new HashMap<String, Object>(1);
        anMap.put("protocolListId", anContractProtocoListId);
        return this.deleteByExample(anMap);
    }

    /**
     * 
     * @Title: queryAllContractSignatory 
     * @Description: 协议关联的签署方信息 
     * @param anContractProtocoListId
     * @return 协议关联的签署方信息 
     * @throws 
     * @author xuzx
     * @date 2017年10月31日 下午4:53:43
     */
    public List<ContractSignatory> queryAllContractSignatory(final Long anContractProtocoListId) {
        Map<String, Object> anMap = QueryTermBuilder.newInstance().build();
        anMap.put("protocolListId", anContractProtocoListId);
        return this.selectByProperty(anMap);

    }

    /**
     * 
     * @Title: queryCountContractSignatory 
     * @Description: 根据协议清单编号返回签署方总数
     * @param anContractProtocoId  协议清单编号
     * @return 计算签署方总数
     * @throws 
     * @author xuzx
     * @date 2017年11月5日 下午5:18:21
     */
    public int queryCountContractSignatory(final Long anContractProtocoId) {
        Map<String, Object> anMap = new HashMap<String, Object>(1);
        anMap.put("protocolListId", anContractProtocoId);
        return this.selectByClassProperty(ContractSignatory.class, anMap).size();

    }
}
